package com.itheima.service.impl;

import com.itheima.dao.FileDao;
import com.itheima.pojo.File;
import com.itheima.service.FileService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    FileDao fileDao;

    @Autowired(required=false)
    public IdWorker idWorker;

    @Override
    public List<File> pictures(HttpServletRequest request, List<MultipartFile> filedatas) {
        //如果是用户上传，则包含用户uid
        String userUid = "kehaonan";
        //如果是管理员上传，则包含管理员uid
        String adminUid = "kehaonan";

        /**
         * 需要判断上传的对象是否是管理员或者用户,是才可以上传.等登录完成后接着开发
         */

        //用于储存上传过的文件名
        ArrayList<File> arrayList = new ArrayList<>();

        //遍历图片数据
        for (MultipartFile imgFile : filedatas) {
            File file = new File();


            //获取上传文件的格式,(将文件名以点切割得到)
            //获取文件名
            String originalFilename = imgFile.getOriginalFilename();
            file.setFileOldName(originalFilename);

            //切割
            String substringFilename = originalFilename.substring(originalFilename.lastIndexOf("."));
            String picExpandedName = substringFilename.substring(1);
            file.setPicExpandedName(picExpandedName);

            //产生一个唯一的UID
            String uid = String.valueOf(idWorker.nextId());
            file.setUid(uid);

            //产生一个唯一的文件名
            String filename = uid+substringFilename;
            file.setPicName(filename);

            //设置文件大小
            file.setFileSize(imgFile.getSize());

            //设置文件路径
            String picUrl = "http://q8rgrfipc.bkt.clouddn.com/"+filename;
            file.setPicUrl(picUrl);
            file.setQiNiuUrl(picUrl);

            /**
             * 下面两个应该在用户登录后在请求中获取
             */
            file.setAdminUid(userUid);
            file.setUserUid(userUid);

            file.setStatus(1);
            file.setCreateTime(DateUtils.getNowTime());
            file.setUpdateTime(DateUtils.getNowTime());


            //将文件保存到七牛云上
            try {
                QiniuUtils.upload2Qiniu(imgFile.getBytes(),filename);
                //获取成功后的所有图片信息返回
                arrayList.add(file);
                fileDao.insert(file);

            } catch (IOException e) {
                e.printStackTrace();
            }


        }

        return arrayList;
    }

    @Override
    public List<File> getPicture(String fileIds, String code) {
        if (fileIds == null || "".equals(fileIds)){
            throw new RuntimeException("没有传入文件id");
        }

        ArrayList<File> list = new ArrayList<File>();
        //先切割获取每一个id
        String[] split = fileIds.split(code);

        if (split.length == 0){
            File file = fileDao.selectByPrimaryKey(fileIds);
            list.add(file);
        }else {
            for (String fileId : split) {
                File file = fileDao.selectByPrimaryKey(fileId);
                list.add(file);
            }
        }

        return list;
    }
}
